{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "from collections import Counter\n",
    "import binascii"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# read text to be encoded\n",
    "with open('text.txt') as f:\n",
    "    text = f.read()\n",
    "    raw_text = binascii.unhexlify(text.encode())\n",
    "    assert b'flag{' in raw_text\n",
    "    text = text[::-1]\n",
    "\n",
    "# read translation table\n",
    "with open('table.pickle', 'rb') as f:\n",
    "    table = pickle.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# check translation table\n",
    "for char1, code1 in table:\n",
    "    for char2, code2 in table:\n",
    "        if char1!=char2:\n",
    "            assert not code1.startswith(code2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({'e': 70,\n",
       "         '2': 274,\n",
       "         '9': 81,\n",
       "         '4': 124,\n",
       "         '7': 311,\n",
       "         '1': 77,\n",
       "         '6': 649,\n",
       "         'd': 35,\n",
       "         'f': 69,\n",
       "         '0': 208,\n",
       "         '5': 119,\n",
       "         'c': 35,\n",
       "         'a': 3,\n",
       "         '8': 55,\n",
       "         '3': 94,\n",
       "         'b': 6})"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# check char frequency\n",
    "cnt = Counter()\n",
    "for c in text:\n",
    "    cnt[c] += 1\n",
    "cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1000001110011011010010110001111001001101110101011111111000011001111100101111100111111110000110011111011110001111001001110011110000111011100111100010110010100110000110011110101011010010100001110010110110011111001011110001011001001110001110101010100111100001101110101011111111010100110000110010101001111101111000111000101000011011110011111011010000110100111000011100011100001100101010010111001111100001101011110111010100101001011001111100101111001111000011100001101011111001001110010011010111100101100001110000111001111000011010011100001100111010101111111100001110000111000111001011000011101111000011100111101001101011110010110000111000011100011110010011111111110011101010101100001111110101111000011001010110010011011101010111111110000110011111110110011110100101100011111101100111101110100111101001100001110011101100011101001101011110100101000011100000110010101001111101111000111000101000011011110011111011010000111001011111000111011101001111111010011110010101000011010011100001110001110000110010101100101011101011110101111011110100101010101010011110100101000011100111011000011100011110010011000011100001110011110000110100110011110111110011110111010010110010101001111010011000011001010110011110000110100101100111100001110010110111001110101001011001111001011100111110010111100010110010011100111100101010000110100111000011100011100001100111100101110000111000111101110011111001011110011110000110010101010010110011110000111111010111100001100111100101010011101100011100101100111101111000011100000110100101100111100001111110101111000011111111110101111000011100111011000011100001100111100010100001110001110000110011111001010111100011110010011000011010010110101110000110100111001111010011000011001111010101100001101001110000111000111000011100101101110010011010101101001011001111011101010111110111100101111100011100001110000111000111000011010010100101010011101100101001100001100101011000111101101000011001111001010100111011000111001011001111011110000111001011011010010110011110000111011110000111001111010010110000110011110100101100011100010100001101110101011110000111011110000111001111010010101010111011110011110111011001110100010101011110010110000110111010011110100111001111001010110000110101111001011000011010010110101110000110100111001111010011000011001111010101100001100111110010011100111101001010000110011110101011010010100001101001011000111000011100000111011110000111001111010011010111100101100001110000111000111100100111111111100111010101011000011001010110001110000111000011101101010111110000111001010111000011001111100100111000111001011001111011110000110010101100111101010110100101000011100000110110010001100101001100101000100100000111000011100111100001110010011010101101001011001111011101010111110111100101111100011100001100101011001111010101100001110111100001110011110111110011110111010010110010101001111010011000011011011001010100111101001101011110010110000111001110100101110000111000111010011100001100111010100110011110111010000111001001100111011001001110011111000011100111110010011000011111101011110000111000011010111110011110100101001011001110101110101001010010101100001101011110010110000110011110101011010010100001101110101011111111000011010011010111101010110100101001111100100110000111000111011011000011011101001111000101100011100010100001100111101010110100101000011010011001111010101100101011001111100101111011110011101000101000011001111010101100001110010010110000111000111100100111111111100111010101011010110011100111011000100101000100101000111001111011101010110011001010110001001001011001100101011000100100101011010010101011001100101010100101100011101110110111100001101011110010110101100111011010000010101101100101011011011110001111001011111111000011100000111001001110011110101011000011010111101001010000110011111001001110001110010110000111001001101010110100101100111101110101011111011110010111110001110000110011110101011010010100001110010110111000011100011100101100001101010110010101100011101110101001010000110011110101011010010100001101001011001111010101100001100101010111010011110001011000111000101000011001111010101101001010000111111010111100001100111110000110101111000011001010110001110000111000001110010011001111100101111011110101111011101000101000011001111010101101001010000111000011010111100001100101011011110000111001111010101100101101001011000111011101001011001010100001100101011001111010101100001110011101100011110110110001110000111011110000111001111101101010111101110101010110100101000011101111000011100111101001010111011000111010010100101010000110100111000011100011100001110010011100011101010101001111000011001111010101101001010000110111010101111111100001110111100001110011111001110101001100111010100101001010100001101001010111011000111010010100101010000110101111010010100001100111111101100011101010110000110100111010110011110101011000011010011001111100101001011001111100101111100011100111101110100001101001110011111110110001110100110000111001011011001111001010101011111001011110010110000111000111000011010111101001010000111011010011110111010100110000111001001101110100111101001010000110011110101011010010100001100101011000111000011100000111000011010111110011110100101001110110010111101011110010101000011010010100101010011110111100001100111101010110100101000011101101001111100001110010101110000110011111001001110011110100101000011010010110001110101011010010100001101001011000111000011001111100001101011110000110101111100001100001101001011000111010101101001010000110010101100111100001110010011100111101010110000111001011111001011110011110100101000011010010110101110000110100111001111010011000011011101010111100101010010101001111111101011110111010001010000110010101100111101010110000110100101100011101010111011010000111000001110010011001111100101111011110101111011101000101000011011011001111010011010111100101100001101001010010101001111100101111100101111100011110010011001010101101100001100111101010110100101000011010111101001010000111000011010111110011110100101001110110010111101011110010101000011100011100001101001110000111001111111100001101011110100101000011010011001111100111101110101001010000110011111001011110010111000011001110100001110011101100100110000111001011011010010100111010101111000011100111011000111101101000011100111010010101100011100111100001100111101010110100101000011001010110001111011010000110100101010101110111001110101011110101011010010100001100111101010110000110100101100011101010111011010000110111010101111111100001110111100001110011110100101000101010111100001110000011100100111000111010101010011110000111001011111000111100001110011111111000011001111010101101001010000111011110000111001111100101011110001110100101000011111101011110000110100111000111001111010010100101011000011100111100001110010011001111100101111011110101111011101000101000011010010110010111100111010010111001111111111111001111010011000011100011100001110111100001110011111110110010111101011110010101000011111101011110000111000011010111110011110100101000101010111100001100111101010110100101000011001010101001011000011001111010011001110101001010010101000011001010110011110101011000011001111111011000111101110000110111010101111001010100101010011111111010111101110100010100001110001110000111001011011001111001111011101101110011110100110000110010101011101001111010010100101011000111100100110000110010101100111101010110000111011110000111001111010010101001010011111011100001100111110010111110011110101011101101'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encode text\n",
    "text.translate({ord(k):v for k,v in table})"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
